﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace TimeSheet.UI.Views
{
    /// <summary>
    /// Class to display a week footer (total of each days)
    /// </summary>
    public class WeekDayFooter : INotifyPropertyChanged
    {
        #region EVENTS

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region DECLARATIONS

        private ObservableCollection<WeekProjectView> _projects = null;
        private string _totalMonday = string.Empty;
        private string _totalTuesday = string.Empty;
        private string _totalWednesday = string.Empty;
        private string _totalThursday = string.Empty;
        private string _totalFriday = string.Empty;
        private string _totalSaturday = string.Empty;
        private string _totalSunday = string.Empty;

        #endregion

        #region CONSTRUCTORS

        /// <summary>
        /// Initialize a new instance of WeekDayFooter based on the project week days
        /// </summary>
        /// <param name="projects"></param>
        public WeekDayFooter(ObservableCollection<WeekProjectView> projects)
        {
            _projects = projects;

            // Catch PropertyChanged event to calculate the total of each days
            _projects.CollectionChanged += (sender, e) =>
            {
                foreach (WeekProjectView item in e.NewItems)
                {
                    item.PropertyChanged += (sender2, e2) =>
                    {
                        SetTotalOfDays();
                    };
                }
                SetTotalOfDays();
            };

        }

        #endregion

        #region PROPERTIES

        /// <summary>
        /// Sets or gets the total days for Monday
        /// </summary>
        public string TotalMonday
        {
            get
            {
                return _totalMonday;
            }
            set
            {
                _totalMonday = value;
                OnPropertyChanged("TotalMonday");
            }
        }

        /// <summary>
        /// Sets or gets the total days for Tuesday
        /// </summary>
        public string TotalTuesday
        {
            get
            {
                return _totalTuesday;
            }
            set
            {
                _totalTuesday = value;
                OnPropertyChanged("TotalTuesday");
            }
        }

        /// <summary>
        /// Sets or gets the total days for Wednesday
        /// </summary>
        public string TotalWednesday
        {
            get
            {
                return _totalWednesday;
            }
            set
            {
                _totalWednesday = value;
                OnPropertyChanged("TotalWednesday");
            }
        }

        /// <summary>
        /// Sets or gets the total days for Thursday
        /// </summary>
        public string TotalThursday
        {
            get
            {
                return _totalThursday;
            }
            set
            {
                _totalThursday = value;
                OnPropertyChanged("TotalThursday");
            }
        }

        /// <summary>
        /// Sets or gets the total days for Friday
        /// </summary>
        public string TotalFriday
        {
            get
            {
                return _totalFriday;
            }
            set
            {
                _totalFriday = value;
                OnPropertyChanged("TotalFriday");
            }
        }

        /// <summary>
        /// Sets or gets the total days for Saturday
        /// </summary>
        public string TotalSaturday
        {
            get
            {
                return _totalSaturday;
            }
            set
            {
                _totalSaturday = value;
                OnPropertyChanged("TotalSaturday");
            }
        }

        /// <summary>
        /// Sets or gets the total days for Sunday
        /// </summary>
        public string TotalSunday
        {
            get
            {
                return _totalSunday;
            }
            set
            {
                _totalSunday = value;
                OnPropertyChanged("TotalSunday");
            }
        }

        #endregion

        #region PRIVATES

        /// <summary>
        /// Manage the totals for each days
        /// </summary>
        private void SetTotalOfDays()
        {
            TimeSpan totalMonday = new TimeSpan();
            TimeSpan totalTuesday = new TimeSpan();
            TimeSpan totalWednesday = new TimeSpan();
            TimeSpan totalThursday = new TimeSpan();
            TimeSpan totalFriday = new TimeSpan();
            TimeSpan totalSaturday = new TimeSpan();
            TimeSpan totalSunday = new TimeSpan();

            foreach (WeekProjectView item in _projects)
            {
                totalMonday += item.Monday.ToTime() ?? TimeSpan.Zero;
                totalTuesday += item.Tuesday.ToTime() ?? TimeSpan.Zero;
                totalWednesday += item.Wednesday.ToTime() ?? TimeSpan.Zero;
                totalThursday += item.Thursday.ToTime() ?? TimeSpan.Zero;
                totalFriday += item.Friday.ToTime() ?? TimeSpan.Zero;
                totalSaturday += item.Saturday.ToTime() ?? TimeSpan.Zero;
                totalSunday += item.Sunday.ToTime() ?? TimeSpan.Zero;
            }

            this.TotalMonday = totalMonday.ToHoursMinutes();
            this.TotalTuesday = totalTuesday.ToHoursMinutes();
            this.TotalWednesday = totalWednesday.ToHoursMinutes();
            this.TotalThursday = totalThursday.ToHoursMinutes();
            this.TotalFriday = totalFriday.ToHoursMinutes();
            this.TotalSaturday = totalSaturday.ToHoursMinutes();
            this.TotalSunday = totalSunday.ToHoursMinutes();
        }

        /// <summary>
        /// Raise the PropertyChanged event
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
